using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using dender.dvdcover.core;
using System.Drawing;
using dender.dvdcover.core.image;

namespace dender.dvdcover.gui.custom
{
    /// <summary>
    /// Defines a Picturebox with a part reference
    /// </summary>
    public class PartPicturebox : PictureBox
    {
        private bool mCenterResized = false;

        private ContextMenuStrip partMenuStrip;
        private System.ComponentModel.IContainer components;
        private ToolStripMenuItem browseToolStripMenuItem;              //Browse
        private ToolStripSeparator seperatorOne;                        //---------------
        private ToolStripMenuItem centeredCropResizeToolStripMenuItem;  //Crop Resize

        private CoverPart mPart;
        /// <summary>
        /// Gets or Sets the part of the PartPictureBox
        /// </summary>
        public CoverPart Part
        {
            get { return mPart; }
            set { mPart = value; }
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.partMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.browseToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.seperatorOne = new System.Windows.Forms.ToolStripSeparator();
            this.centeredCropResizeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.partMenuStrip.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // partMenuStrip
            // 
            this.partMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.browseToolStripMenuItem,
            this.seperatorOne,
            this.centeredCropResizeToolStripMenuItem});
            this.partMenuStrip.Name = "contextMenuStrip1";
            this.partMenuStrip.Size = new System.Drawing.Size(166, 104);
            // 
            // browseToolStripMenuItem
            // 
            this.browseToolStripMenuItem.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.browseToolStripMenuItem.Name = "browseToolStripMenuItem";
            this.browseToolStripMenuItem.Click +=new EventHandler(browseToolStripMenuItem_Click);
            this.browseToolStripMenuItem.Size = new System.Drawing.Size(165, 22);
            this.browseToolStripMenuItem.Text = "Load Image...";
            // 
            // toolStripMenuItem2
            // 
            this.seperatorOne.Name = "toolStripMenuItem2";
            this.seperatorOne.Size = new System.Drawing.Size(162, 6);
            // 
            // centeredCropResizeToolStripMenuItem
            // 
            this.centeredCropResizeToolStripMenuItem.Name = "centeredCropResizeToolStripMenuItem";
            this.centeredCropResizeToolStripMenuItem.Click +=new EventHandler(centeredCropResizeToolStripMenuItem_Click);
            this.centeredCropResizeToolStripMenuItem.Size = new System.Drawing.Size(165, 22);
            this.centeredCropResizeToolStripMenuItem.Text = "Crop Resize";

            this.partMenuStrip.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

            this.ContextMenuStrip = partMenuStrip;

        }

        public PartPicturebox(CoverPart part) : base()
        {
            InitializeComponent();
            this.BorderStyle = BorderStyle.None;
            this.Part = part;
            this.ContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuStrip_Opening);
            this.Part.OnPartImageChanged += new EventHandler<PartImageChangedEventArgs>(OnPartImageChanged);
        }     
        /// <summary>
        /// Loads an image into the PictureBox
        /// </summary>
        /// <param name="image"></param>
        public void LoadImage(Bitmap image)
        {
            CoverImage coverImage = new CoverImage(this.Part,image);
            mPart.Fill(coverImage);
        }

        /// <summary>
        /// Checks if the PictureBox contains an image
        /// </summary>
        /// <returns>True when the Part holds a CoverImage</returns>
        public bool HasImage
        {
            get
            {
                return (this.Part.Image != null);
            }
        }

        /// <summary>
        /// Use the CenterResize algorithm to resize the image of the Part
        /// </summary>
        public void CenterResize()
        {
            if (this.HasImage)
            {
                if (!mCenterResized)
                {
                    mCenterResized = true;
                    this.Part.Image.Resize(new CenterResizeStyle());
                }
                else
                {
                    ResetTransforms();
                }
            }
        }

        /// <summary>
        /// Resets the Transform of the image of the part
        /// </summary>
        public void ResetTransforms()
        {
            if (this.HasImage)
            {
                mCenterResized = false;
                this.Part.Image.ResetTransform();
            }
        }

        /// <summary>
        /// Initializes and shows the OpenDialog
        /// </summary>
        /// <returns>Filename of the selected file, null if action was canceld</returns>
        public virtual string ShowOpenDialog()
        {
            string returnValue = null;
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.CheckFileExists = true;
            openDialog.Multiselect = false;
            openDialog.Filter = "JPEG files|*.jpg|" +
                                "BMP files|*.bmp";

            DialogResult res = openDialog.ShowDialog();
            if (res == DialogResult.OK)
            {
                returnValue = openDialog.FileName;
            }
            return returnValue;
        }

        private void browseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filename = ShowOpenDialog();
            if (filename != null)
            {
                Bitmap image = new Bitmap(filename);
                this.LoadImage(image);
            }
        }

        private void centeredCropResizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.CenterResize();
        }

        private void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            centeredCropResizeToolStripMenuItem.Checked = mCenterResized;
        }

        private void OnPartImageChanged(object sender, PartImageChangedEventArgs e)
        {
            if (e.ValidImage)
            {
                this.Image = e.NewImage.Image;
            }
            else
            {
                this.Image = null;
            }
            this.Refresh();
        }
    }
}
